package com.ruoyi.bizsys.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bizsys.domain.ProductDeferredInfo;
import com.ruoyi.bizsys.service.IProductDeferredInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 金融产品包Controller
 *
 */
@RestController
@RequestMapping("/business/ProductDeferred")
public class ProductDeferredInfoController extends BaseController {
    @Autowired
    private IProductDeferredInfoService productDeferredInfoService;


    @GetMapping("/getlist")
    public TableDataInfo getList( ProductDeferredInfo productDeferredInfo) {
        startPage();
        productDeferredInfo.setState("1");
        return getDataTable(productDeferredInfoService.getlist(productDeferredInfo));
    }

    @PostMapping("/getone")
    public AjaxResult getOne(@RequestBody JSONObject jsonObject) {
        if(StringUtils.isEmpty(jsonObject.get("id"))){
            throw new BusinessException(RspEnmu.DEFERRED_PRODUCT_IDISNULL);
        }
        return AjaxResult.success(productDeferredInfoService.getOne(new QueryWrapper<ProductDeferredInfo>().eq("id",jsonObject.get("id"))));
    }
    @PostMapping("/updateone")
    public AjaxResult updateone(@RequestBody ProductDeferredInfo productDeferredInfo){
        if(StringUtils.isEmpty(productDeferredInfo.getId())||
                StringUtils.isEmpty(productDeferredInfo.getProductCode())||
                StringUtils.isEmpty(productDeferredInfo.getProductName())||
                StringUtils.isEmpty(productDeferredInfo.getExpireTime())
            ){
            throw new BusinessException(RspEnmu.DEFERRED_PRODUCT_IDISNULL);
        }
        if(productDeferredInfo.getExpireTime().before(new Date())){
            throw  new BusinessException(RspEnmu.INVALID_EXPIRATION_TIME);
        }
        return AjaxResult.success(productDeferredInfoService.updateById(productDeferredInfo));
    }
    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody ProductDeferredInfo productDeferredInfo){
        if(!StringUtils.isEmpty(productDeferredInfo.getExpireTime())){
            if(productDeferredInfo.getExpireTime().before(new Date())){
                throw  new BusinessException(RspEnmu.INVALID_EXPIRATION_TIME);
            }
        }
        productDeferredInfo.setState("1");
        return AjaxResult.success(productDeferredInfoService.insertProductDeferredInfo(productDeferredInfo));
    }
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids){
        return AjaxResult.success(productDeferredInfoService.deleteProductDeferredInfo(ids));
    }

}